package com.woniu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.bean.CmsContent;
import com.woniu.bean.CmsType;
import com.woniu.bean.R;
import com.woniu.service.ICmsContentService;
import com.woniu.service.ICmsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yellowDocter
 * @since 2022-08-06
 */
@RestController
@CrossOrigin
@RequestMapping("/cmsContent")
public class CmsContentController {
    @Autowired
    private ICmsContentService iCmsContentService;
    @Autowired
    private ICmsTypeService iCmsTypeService;

    @GetMapping("type")
    public R findType(){
        List<CmsType> list = iCmsTypeService.list();
        R r = new R();
        r=R.ok();
        Map map=new HashMap<>();
        map.put("records", list);
        r.setData(map);
        return r;
    }
    @GetMapping
    public R selectall(Integer currentPage, Integer pageSize) {
        System.out.println("currentPage = " + currentPage);
        System.out.println("pageSize = " + pageSize);
        R r= null;
        IPage<CmsContent> iPage=new Page<>(currentPage,pageSize);
        IPage<CmsContent> page = iCmsContentService.page(iPage);
        List<CmsContent> records = page.getRecords();
        records.forEach(e->{
            CmsType byId = iCmsTypeService.getById(e.getTypeId());
            e.setCmsType(byId);
        });
        System.out.println("aaaa = " + records);
        long total = page.getTotal();
        r=R.ok();
        Map map=new HashMap<>();
        map.put("records", records);
        map.put("total", total);
        r.setData(map);
        return r;
    }
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable Integer id) {

        R r=null;
        boolean b = iCmsContentService.removeById(id);
        if(b){
            r=R.ok();
        }

        return r;
    }
    @DeleteMapping
    public R deleteAll(@RequestBody List<Integer> ids) {
        System.out.println(ids);
        R r=null;
        boolean b = iCmsContentService.removeBatchByIds(ids);
        if(b){
            r=R.ok();
        }else {
            r=R.error();
        }

        return r;
    }
    @PostMapping
    public R add(@RequestBody CmsContent building) {
        System.out.println("HouseController.add");
        boolean save = iCmsContentService.save(building);
        R r=null;
        if(save){
            r=R.ok();
        } else {
            r=R.error();
        }
        return r;
    }
    @PutMapping
    public R updateCms(@RequestBody CmsContent cmsContent) {
        System.out.println("cmsContent = " + cmsContent);
        boolean b = iCmsContentService.updateById(cmsContent);
        R r=null;
        if(b){
            r=R.ok();
        } else {
            r=R.error();
        }
        return r;
    }
    @GetMapping("/{id}")
    public CmsContent selectById(@PathVariable Integer id) {
        CmsContent cmsContent = iCmsContentService.getById(id);
        return cmsContent;
    }
    @GetMapping("name")
    public R selectByname(@RequestParam String title,String text,Integer currentPage, Integer pageSize){

        System.out.println("title = " + title);
        IPage<CmsContent> iPage=new Page<>(currentPage,pageSize);
        QueryWrapper<CmsContent> qr = new QueryWrapper<>();
        if (StringUtils.hasText(text)) {
            qr.like("content_text",text);
        };
        if (StringUtils.hasText(title)) {
            qr.like("content_title",title);
        }
        IPage<CmsContent> page = iCmsContentService.page(iPage, qr);
        List<CmsContent> records = page.getRecords();
        System.out.println("records = " + records);
        long total = page.getTotal();
        R r=R.ok();

        Map map=new HashMap<>();
        map.put("list", records);
        map.put("total", total);
        r.setData(map);
        return r;
    }
}
